const WebSocket = require('ws')
import { wsData } from '../webSocketData'
// 创建服务器对象
const wss = new WebSocket.Server({
  port: 3001
})

export var tower_obj = {}

export function listen () {
  wss.on('connection', client => {
    client.on('message', (msg) => {
      tower_obj = JSON.parse(msg)
      console.log('接收到客户端数据', tower_obj)
      // client.send('接收数据成功')
    })
    // console.log('66666666', wss.clients.size)
    let timer = setInterval(async () => {
      let dt = await wsData()
      client.send(JSON.stringify(dt))
    }, 2000)
    if (wss.clients.size < 1) {
      clearInterval(timer)
    }
  })
}
